package com.klikmee.sms.core.base;

import java.util.Date;
import java.util.List;

import com.klikmee.sms.core.base.dao.OutgoingMessageDAO;
import com.klikmee.sms.core.base.dao.PaymentInvoiceDAO;
import com.klikmee.sms.core.base.util.Constants;
import com.klikmee.sms.core.model.OutgoingMessage;
import com.klikmee.sms.core.model.PaymentInvoice;

public class Maintenance {

	private OutgoingMessageDAO	outgoingMessageDAO;
	private Core				core;
	private PaymentInvoiceDAO	paymentInvoiceDAO;

	/**
	 * Scheduled method for handling unsent messages. Picks up messages not older than 10 minutes and tries to resend them. If a message is older then 10 minutes marks them as time outed.
	 */
	public void retrySending() {/*

		// find all outgoing messages in state WAITING TO BE SENT and send them
		List<OutgoingMessage> omWaitingToBeSent = outgoingMessageDAO.findMessagesForSending();
		Date tenMinutesAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 10);
		Date oneMinutesAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 1);

		for (OutgoingMessage outgoingMessage : omWaitingToBeSent) {
			// if message is older then minute and less then 10 minutes old try
			// to send it again
			if (outgoingMessage.getStateChangeTime().after(tenMinutesAgo) && outgoingMessage.getStateChangeTime().before(oneMinutesAgo)) {
				if (!outgoingMessage.getPaymentInvoiceList().isEmpty()) {
					PaymentInvoice paymentInvoice = outgoingMessage.getPaymentInvoiceList()p.iterator().next();
					switch (paymentInvoice.getType()) {
					case ConstantManager.PI_TYPE_FREE_SMS:
					case ConstantManager.PI_TYPE_SMS_BULK:
						core.sendMessage(outgoingMessage);
						break;
					case ConstantManager.PI_TYPE_CHARGED_MESSAGE:
						if (outgoingMessage.getState().equals(ConstantManager.OM_WAITING_TO_BE_SENT)) {
							core.sendMessage(outgoingMessage);
						}
					default:
						break;
					}

				}
			} else if (outgoingMessage.getStateChangeTime().before(tenMinutesAgo)) {
				// if message couldn't be sent in 10 minutes and 10 trys mark it
				// as not sent because of timeout
				switch (outgoingMessage.getState()) {
				// FREE & BULK MESSAGES
				case ConstantManager.OM_CREATED:
				case ConstantManager.OM_WAITING_TO_BE_SENT: {
					outgoingMessage.setState(ConstantManager.OM_NOT_SENT);
					outgoingMessageDAO.edit(outgoingMessage);
					if (!outgoingMessage.getPaymentInvoiceList().isEmpty()) {
						PaymentInvoice paymentInvoice = outgoingMessage.getPaymentInvoiceList().iterator().next();
						if (paymentInvoice.getType() == ConstantManager.PI_TYPE_FREE_SMS) {
							paymentInvoice.setState(ConstantManager.PI_FREE_MESSAGE_SENDING_TIMED_OUT);
						} else if (paymentInvoice.getType() == ConstantManager.PI_TYPE_SMS_BULK) {
							paymentInvoice.setState(ConstantManager.PI_BULK_MESSAGE_SENDING_TIMED_OUT);
						}

						paymentInvoiceDAO.edit(paymentInvoice);
					}

					break;
				}
					// PAYMENT MESSAGES
				case ConstantManager.OM_PAYMENT_MESSAGE_WAITING_TO_BE_SENT: {

					outgoingMessage.setState(ConstantManager.OM_PAYMENT_MESSAGE_SENDING_TIMED_OUT);
					outgoingMessageDAO.edit(outgoingMessage);

					PaymentInvoice paymentInvoice = outgoingMessage.getPaymentInvoiceList().iterator().next();
					paymentInvoice.setState(ConstantManager.PI_PAYMENT_MESSAGE_SENDING_TIMED_OUT);
					paymentInvoiceDAO.edit(paymentInvoice);
					break;
				}
				default:
					break;
				}

			}
		}
*/
	}
}
